from django.shortcuts import render
from django.shortcuts import render
import django_filters
from django_filters import FilterSet
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from phone import models, serializers
import re
# Create your views here.
class PhoneViewSet(viewsets.ModelViewSet):
    queryset = models.Phone.objects.all()
    serializer_class = serializers.PhoneSerializer

    @action(detail=False, methods=['post'])
    def search(self, request):
        a = request.data['phone']
        b = request.data['password']
        if re.match(r'^1[3-9]\d{9}$', a):
            data = models.Phone.objects.filter(phone=a,password=b)
            if len(data) > 0:
                return Response({"code":200,'message': '登录成功'})
            else:
                return Response({'message': '用户名或密码错误'})
        else:
            return Response({'message': '手机号格式错误'})

